package per.xue.carid_booking.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import per.xue.carid_booking.dto.respondDTO.CarRespondDTO;
import per.xue.carid_booking.utils.KeyUtil;
import per.xue.carid_booking.base.utils.ResultUtil;
import per.xue.carid_booking.base.dto.ResultDTO;
import per.xue.carid_booking.base.page.PageList;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import per.xue.carid_booking.entity.Station;
import per.xue.carid_booking.service.CarService;
import per.xue.carid_booking.entity.Car;
import per.xue.carid_booking.service.StationService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 *
 * @author ${author}
 * @since 2021-05-22
 */
@RestController
@RequestMapping("/car")
@CrossOrigin
@Slf4j
public class CarController {

        @Autowired
        private CarService carService;

        @Autowired
        private StationService stationService;

        @PostMapping("/insertCar")
        @ApiOperation(value = "新增一个车次")
/*        @ApiImplicitParams({
                @ApiImplicitParam(name = "startStationId", dataType = "Integer", required = true, value = "出发站点id"),
                @ApiImplicitParam(name = "endStationId", dataType = "Integer",  required = true, value = "终点站点id"),
                @ApiImplicitParam(name = "price", dataType = "double",  required = true, value = "票价"),
                @ApiImplicitParam(name = "surplusNum", dataType = "Integer",  required = true, value = "剩余量"),
                @ApiImplicitParam(name = "time", dataType = "string",  required = true, value = "发车时间,格式：yyyy-MM-dd HH:mm")
        })*/
        public ResultDTO insertOne( @RequestBody Car car)throws Exception{
            car.setCarId(KeyUtil.getUniqueKey());
            car.setStats("0");
            try {
                carService.save(car);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(car);
        }

        @PostMapping("/updateById")
        @ApiOperation(value = "按id修改数据")
        //@ApiImplicitParams()
        public ResultDTO updateById( @RequestBody Car car)throws Exception{
            try {
                Car car1 = carService.get(car.getCarId());
                if (car1.getTime().compareTo(new Date()) == -1) {
                    return ResultUtil.Error("500","该车次已发车");
                }
                carService.update(car);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(carService.get(car.getCarId()));

        }

        @PostMapping("/UserGetCar")
        @ApiOperation("用户获取某一天的车次")
/*        @ApiImplicitParams({
                //@ApiImplicitParam(name = "startStationId", dataType = "Integer", required = false, value = "出发站点id"),
                //@ApiImplicitParam(name = "endStationId", dataType = "Integer",  required = false, value = "终点站点id"),
                @ApiImplicitParam(name = "time", dataType = "string",  required = true, value = "查当天车次准确到分钟，查明天的车次时分传0，格式：yyyy-MM-dd HH:mm，")
        })*/
        public ResultDTO UserGetCar( @RequestBody Car car)throws Exception{
            List<CarRespondDTO> result = new ArrayList<>();
            try {
                List<Car> carList = carService.UserFindCar(car,car.getTime());

                //增加站点名称
                for (Car item : carList) {
                    Station startStation = stationService.get(item.getStartStationId());
                    Station endStation = stationService.get(item.getEndStationId());
                    CarRespondDTO carRespondDTO = new CarRespondDTO(item, startStation.getStationName(), endStation.getStationName());
                    result.add(carRespondDTO);
                }
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(result);
        }

        @PostMapping("/AdminGetCar")
        @ApiOperation("管理员获取某一天的车次")

        public ResultDTO AdminGetCar( @RequestBody Car car)throws Exception{
            List<CarRespondDTO> result = new ArrayList<>();
            try {
                List<Car> carList = carService.AdminFindCar(car,car.getTime());
                //增加站点名字
                for (Car item : carList) {
                    Station startStation = stationService.get(item.getStartStationId());
                    Station endStation = stationService.get(item.getEndStationId());
                    CarRespondDTO carRespondDTO = new CarRespondDTO(item, startStation.getStationName(), endStation.getStationName());
                    result.add(carRespondDTO);
                }
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(result);
        }



        @GetMapping("/deleteById/{id}")
        @ApiOperation("按id删除数据")
/*        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "path", name = "id", dataType = "string", required = true, value = "实体类id"),

        })*/
        public ResultDTO deleteById( @PathVariable("id") String id)throws Exception{
            Car oldCar = null;
            try {
                oldCar = carService.get(id);
                carService.deleteById(id);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(oldCar);
        }

        @PostMapping("/getCarPage")
        @ApiOperation("按条件查询，分页")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "pageNum", dataType = "Integer", defaultValue = "1", required = false, value = "页码"),
                @ApiImplicitParam(name = "pageSize", dataType = "Integer", defaultValue = "10", required = false, value = "每页容量")
        })
        public ResultDTO selectEntityPage( /*@RequestBody*/ Car car,
                                              int pageNum,
                                              int pageSize)throws Exception{

            PageList<Car> pageData = null;
            try {
                pageData = carService.findPageData(car, pageNum, pageSize);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(pageData);

        }

        @PostMapping("/getCar")
        @ApiOperation("按条件查询，不分页")
        //@ApiImplicitParams()
        public ResultDTO selectEntity( @RequestBody Car car)throws Exception{
            List<CarRespondDTO> result = new ArrayList<>();
            try {
                List<Car> carList = carService.findByParams(car);
                //增加站点名字
                for (Car item : carList) {
                    Station startStation = stationService.get(item.getStartStationId());
                    Station endStation = stationService.get(item.getEndStationId());
                    CarRespondDTO carRespondDTO = new CarRespondDTO(item, startStation.getStationName(), endStation.getStationName());
                    result.add(carRespondDTO);
                }
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(result);
        }



}
